package com.shanling.shanlingcontroller.persenter;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;

import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.blelib.manager.LPAVSBTManager;
import com.linkplay.lpvr.lpvrbean.BaseDevice;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.music.player.SongLoader;
import com.shanling.shanlingcontroller.persenter.contract.BleContract;
import com.shanling.shanlingcontroller.services.csr.GAIABREDRProvider;
import com.shanling.shanlingcontroller.ui.fragment.CUEFragment;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;


import java.lang.reflect.Method;

import io.reactivex.functions.Consumer;
import rx.Observer;

/**
 * Created by ch1 on 2019/10/12.
 */

public class BlePersenter extends RxPresenter<BleContract.View> implements BleContract.Presenter, BluetoothProfile.ServiceListener {
    private BluetoothHeadset bluetoothHeadset;
    private BluetoothA2dp bluetoothA2dp;
    public BlePersenter(Context context) {
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context,this,  BluetoothProfile.HEADSET);
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context,this,  BluetoothProfile.A2DP);
    }
    @Override
    public void checkAppPermission(Activity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.requestEach(Manifest.permission.ACCESS_COARSE_LOCATION
                ,Manifest.permission.ACCESS_FINE_LOCATION
        ).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if(permission.granted){
                    if(mView!=null){
                        mView.getPermissonSuccess();
                    }
                }else {
                    if (!permission.shouldShowRequestPermissionRationale) {
                        if(mView!=null){
                            mView.getPermissonFaild();
                        }
                    }
                }

            }
        });

    }
    @Override
    public void doConnectA2dp(BluetoothDevice device) {
            try {
                Log.e("weiyu","连接a2dp");
                Method mMethodconnect=BluetoothA2dp.class.getMethod("connect",
                        BluetoothDevice.class);
                Method connect = bluetoothHeadset.getClass().getMethod("connect", BluetoothDevice.class);
            //    connect.invoke(bluetoothHeadset,device);
                mMethodconnect.invoke(bluetoothA2dp, device);

            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    @Override
    public void doBand(BluetoothDevice device,int type, Context context) {
        int bondState = device.getBondState();
        if(bondState==10){
            try {
                Method var1 =device.getClass().getMethod("createBond");
                var1.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            if (bluetoothA2dp.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED) {
                doConnectA2dp(device);
            }else {
                doConnectSpp(device,type,context);
            }
        }
    }
    @Override
    public void doConnectSpp(BluetoothDevice device,int type,Context context) {
        switch (type){
            case 1:
                PreferenceUtil.getInstance(context).setBleType(1);
                LPAVSBTManager btManager = LPAVSManager.getInstance(CustomApplication.getInstance()).getBtManager();
                btManager .connect(new BaseDevice(device,btManager,false));
                break;
            case 2:
                PreferenceUtil.getInstance(context).setBleType(2);
                CustomApplication.getInstance().getBluetoothDeviceManager().connect(device);
                break;
            case 3:
                PreferenceUtil.getInstance(context).setBleType(3);
                GAIABREDRProvider gaiabredrProvider = CustomApplication.getInstance().getmGAIABREDRProvider();
                if (gaiabredrProvider.getDevice() == null) {
                    gaiabredrProvider.connect(device.getAddress(), context);
                } else {
                    if(gaiabredrProvider.getDevice().getAddress().equals(device.getAddress())){
                        gaiabredrProvider.reconnectToDevice(context);
                    }else {
                        gaiabredrProvider.connect(device.getAddress(), context);
                    }
                }
                break;
        }


    }

    @Override
    public void onFinish() {
        BluetoothAdapter.getDefaultAdapter().closeProfileProxy( BluetoothProfile.HEADSET,bluetoothHeadset);
        BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP,bluetoothA2dp);
    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        switch (profile){
            case BluetoothProfile.A2DP:
                bluetoothA2dp= (BluetoothA2dp) proxy;
                break;
            case BluetoothProfile.HEADSET:
                bluetoothHeadset= (BluetoothHeadset) proxy;
                break;
        }
    }
    @Override
    public void onServiceDisconnected(int profile) {
        switch (profile){
            case BluetoothProfile.A2DP:
                bluetoothA2dp=null;
                break;
            case BluetoothProfile.HEADSET:
                bluetoothHeadset=null;
                break;
        }
    }
}
